package com.whh.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class NumUtils extends StrUtils {

	public static BigDecimal toBigDecimal(Object obj) {
		if(obj==null) return null;
		if(obj instanceof BigDecimal) return (BigDecimal) obj;
		return BigDecimal.valueOf(Double.valueOf(String.valueOf(obj)));
	}
	
	public static Integer toInteger(Object obj) {
		if(obj==null) return null;
		if(obj instanceof Integer) return (Integer) obj;
		return Integer.valueOf(String.valueOf(obj));
	}
	
	public static List<Integer> toIntegerList(String ids){
		if(isBlank(ids)) return null;
		String[] arrIds = ids.split(",");
		List<Integer> result = new ArrayList<Integer>();
		for(int i=0; i<arrIds.length; i++) {
			String temp = arrIds[i];
			result.add(toInteger(temp));
		}
		return result;
	}
	
	public static BigDecimal getAmount(BigDecimal q,BigDecimal p) {
		if(q==null||p==null) return null;
		BigDecimal a = q.multiply(p).setScale(3, BigDecimal.ROUND_HALF_UP);
		return a;
	}
	/**
	 * 是否奇数
	 * @param a
	 * @return
	 */
	public static Boolean isOdd(Integer a) {
		if((a%2)!=0) return Boolean.TRUE;
		return Boolean.FALSE;
	}
	/**
	 * 是否偶数
	 * @param a
	 * @return
	 */
	public static Boolean isEven(Integer a) {
		return !isOdd(a);
	}
}
